package com.neuedu.shop.service.impl;

import com.neuedu.shop.entity.Category;
import com.neuedu.shop.mapper.CategoryMapper;
import com.neuedu.shop.service.CategoryService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author Qin
 * @since 2021-04-10
 */
@Service
@Transactional
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements CategoryService {
    @Resource
    private CategoryMapper mapper;
    @Override
    public void insertRootCategory(String categoryName, String categoryDescription) {
        mapper.addRootCategory(categoryName,categoryDescription);
    }

    @Override
    public void addChildCategory(String categoryName, String categoryDescription, Integer id) {
System.out.println("Service:"+categoryName+","+categoryDescription+","+id);
        // 1.查
        Category category = mapper.findByCategoryId(id);
        //2.改
        mapper.updateParentLeaf(id);
        //3.加
        Category c = new Category();
        c.setCategoryName(categoryName);
        c.setCategoryDescription(categoryDescription);
        c.setCategoryParentId(id);
        c.setCategoryGrade(category.getCategoryGrade() + 1);
        mapper.addChildCategory(c);
    }

    @Override
    public List<Category> findToThree() {
        return mapper.findToTree();
    }


}